using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using SharpNote.Core.Data;


namespace SharpNote.Core.Data
{
    /// <summary>
    /// 数据库映射对象的基类。
    /// </summary>
    public abstract class DbMappingObjectBase
    {
        /// <summary>
        /// 在数据表中删除指定的对象。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="instance"></param>
        /// <returns></returns>
        public static bool Remove<T>(T instance) where T : new()
        {
            return DbObjectMappingHelper.Remove<T>(instance);
        }

        /// <summary>
        /// 在数据表中创建一个新的对象。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="instance"></param>
        /// <returns></returns>
        public static bool Create<T>(T instance) where T : new()
        {
            return DbObjectMappingHelper.Create<T>(instance);
        }

        /// <summary>
        /// 更新对象到数据表。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="instance"></param>
        /// <returns></returns>
        public static bool Update<T>(T instance) where T : new()
        {
            return DbObjectMappingHelper.Update<T>(instance);
        }

        /// <summary>
        /// 获取表中所有行对象。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="tableName">表名称</param>
        /// <returns></returns>
        public static List<T> SelectAll<T>() where T : new()
        {
            string tableName = GetTableName<T>();
            if (string.IsNullOrEmpty(tableName)) { return null; }

            StringBuilder sql = new StringBuilder();
            sql.AppendLine("select * from " + tableName);

            return DbObjectMappingHelper.QueryObjectList<T>(sql.ToString());
        }

		/*
        /// <summary>
        /// 获取指定页码的结果集。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="rowCountPerpage">每页包含的行数</param>
        /// <param name="pageNumber">要获取的页码</param>
        /// <returns></returns>
        public static List<T> SelectPageOf<T>(long rowCountPerpage, long pageNumber) where T : new()
        {
            string tableName = GetTableName<T>();
            if (string.IsNullOrEmpty(tableName)) { return null; }

            StringBuilder sql = new StringBuilder();
            sql.AppendLine("select * from " + tableName);

            return DbObjectMappingHelper.QueryObjectList<T>(sql.ToString(), rowCountPerpage, pageNumber);
        }*/

        /// <summary>
        /// 获取实体类从属的表名称。
        /// </summary>
        private static string GetTableName<T>()
        {
            object[] attributes = typeof(T).GetCustomAttributes(typeof(TableInfoAttribute), false);

            //获取表名称
            if (attributes == null || attributes.Length == 0) { return string.Empty; }
            TableInfoAttribute tableInfoAttr = attributes[0] as TableInfoAttribute;
            if (tableInfoAttr == null || string.IsNullOrEmpty(tableInfoAttr.TableName)) { return string.Empty; }

            return tableInfoAttr.TableName;
        }

        /// <summary>
        /// 获取表中的行总数。
        /// </summary>
        public static Int64 GetRowCount<T>()
        {
            string tableName = GetTableName<T>();
            if (string.IsNullOrEmpty(tableName)) { return 0; }

            DataAccessService das =  DataAccessService.GetDataAccessServiceInstance();
            object result = das.ExecuteScalar("select count(*) from " + tableName);

            return (result != null && result != DBNull.Value ? Convert.ToInt64(result) : 0);
        }
    }
}
